﻿using Furion;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Furion.Localization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1
{
    public class APIController :IDynamicApiController
    {
        public IStringLocalizer ul;

        public APIController(IStringLocalizer ll)
        {
            ul = ll;
        }

        [HttpGet]
        public dynamic Test(string transtring="中文")
        {
            string r = "";
            if (transtring!= "中文")
            {
                IStringLocalizer xT = App.GetService<IStringLocalizer>();
                r = xT[transtring];
            }
            else
            {
                //Q:Furion中L无法翻译，这个要怎么配置？
                r = L.Text["中文"];
            }
            return r;
        }
        /// <summary>
        /// 异常提醒
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public dynamic errormsg()
        {
            //Q:枚举类型翻译无效？
            throw Oops.Oh(ErrorCode.D1000);
        }
        /// <summary>
        /// 设置语言
        /// </summary>
        /// <param name="LanguageMark"></param>
        /// <returns></returns>
        [HttpPost]
        public dynamic SetLanguage(string LanguageMark)
        {
            if (!string.IsNullOrEmpty(LanguageMark))
                Lang.SetCulture(LanguageMark);
            return "";
        }
        /// <summary>
        /// 获取当前语言
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public dynamic GetLanguage()
        {
            return Lang.GetSelectCulture().Culture.Name;
        }
        /// <summary>
        /// 获取语言列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public dynamic GetLanguageList()
        {
            object returnData = null;
            var data = Lang.GetCultures(); //（Q:没有获取到语言列表数据？）

            //var localizationSettings = app.ApplicationServices.GetRequiredService<IOptions<LocalizationSettingsOptions>>().Value;
            //var localizationSettings = App.GetConfig<LocalizationSettingsOptions>("LocalizationSettings", true);
            //var data = localizationSettings.SupportedCultures;
            if (data != null)
            {
                Dictionary<string, string> dict = new Dictionary<string, string>();
                foreach (var item in data)
                {
                    string languagename = item.Key;
                    switch (item.Key)
                    {
                        case "zh-CN":
                            {
                                languagename = "简体中文";
                                break;
                            }
                        case "en-US":
                            {
                                languagename = "English";
                                break;
                            }
                        default:
                            break;
                    }
                    dict.Add(item.Key, languagename);
                }
                returnData = dict;
            }
            return returnData;
        }
    }
}
